package com.piece.mall.controller;

import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.mall.model.PmsSkuInfo;
import com.piece.mall.model.WmsWareInfo;
import com.piece.mall.model.WmsWareSku;
import com.piece.mall.service.PmsSkuInfoService;
import com.piece.mall.service.WmsShAreaService;
import com.piece.mall.service.WmsWareInfoService;
import com.piece.mall.service.WmsWareSkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = {"仓库管理"})
@RequestMapping("/api/mall/ware")
public class WmsWareInfoController extends FrameWorkController {

    @Resource
    private WmsWareInfoService wmsWareInfoService;

    @Resource
    private WmsShAreaService wmsShAreaService;

    @Resource
    private WmsWareSkuService wmsWareSkuService;

    @Resource
    private PmsSkuInfoService pmsSkuInfoService;

    @PostMapping("/info/list")
    @ApiOperation(value = "查询仓库列表")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public List list() {
        QueryTable queryTable = initTable();
        DataTable dataTable = wmsWareInfoService.findPage(queryTable);
        return dataTable.getData();
    }

    @PostMapping("/info/table")
    @ApiOperation(value = "查询仓库分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse table() {
        QueryTable queryTable = initTable();
        Map<String, Object> params = queryTable.getParams();
        String keyParam = Convert.toString(params.get("Q_key_S_LK"));
        if (StringUtil.isNotEmpty(keyParam)) {
            params.remove("Q_key_S_LK");
            params.put("Q_name_S_LK_OR", keyParam);
            params.put("Q_address_S_LK_OR", keyParam);
        }
        DataTable dataTable = wmsWareInfoService.findPage(queryTable);
        return success(dataTable);
    }

    @GetMapping("/info/load")
    @ApiOperation(value = "加载仓库表单数据")
    @ApiImplicitParam(name="id", value = "主键")
    public AjaxResponse load() {
        Long id = Convert.toLong(getRequest().getParameter("id"));
        WmsWareInfo ware = new WmsWareInfo();
        if (null != id) {
            ware = wmsWareInfoService.findById(id);
            //设置分类路径
            ware.setAreaPath(wmsShAreaService.findPathByZipCode(ware.getAreaCode()));
        } else {
            ware.setAreaCode("0");
            ware.setAreaPath(new String[0]);
        }

        return success(ware);
    }

    @SubmitPermission
    @PostMapping("/info/save")
    @AclPermission(code = "mall:ware:edit")
    @ApiOperation(value = "保存仓库表单数据")
    @ApiImplicitParam(value = "仓库数据", dataTypeClass = WmsWareInfo.class, type = "body")
    public AjaxResponse save() {
        WmsWareInfo wmsWareInfo;
        try {
            Long id = Convert.toLong(getRequest().getParameter("id"));
            if (null != id) {
                wmsWareInfo = wmsWareInfoService.findById(id);
            } else {
                wmsWareInfo = new WmsWareInfo();
            }
            initDB(wmsWareInfo);
            wmsWareInfoService.insertOrUpdate(wmsWareInfo);
            return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS), null);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SAVE_FAILURE), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/info/delete/{id}")
    @AclPermission(code = "mall:ware:delete")
    @ApiOperation(value = "删除仓库数据")
    @ApiImplicitParam(name = "id", value = "主键", type = "path")
    public AjaxResponse delete(@PathVariable String id) throws Exception {
        wmsWareInfoService.delete(id);
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/info/delete")
    @AclPermission(code = "mall:ware:delete")
    @ApiOperation(value = "删除仓库数据")
    @ApiImplicitParam(name = "ids", value="主键集合")
    public AjaxResponse deleteBatch() throws Exception {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            wmsWareInfoService.delete(Convert.toLongArray(ids));
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @PostMapping("/sku/table")
    @ApiOperation(value = "查询商品库存分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse skuTable() {
        QueryTable queryTable = initTable();
        Map<String, Object> params = queryTable.getParams();
        DataTable dataTable = wmsWareSkuService.findPage(queryTable);
        return success(dataTable);
    }

    @GetMapping("/sku/load")
    @ApiOperation(value = "加载商品库存表单数据")
    @ApiImplicitParam(name="id", value = "主键")
    public AjaxResponse loadSku() {
        Long id = Convert.toLong(getRequest().getParameter("id"));
        Long skuId = Convert.toLong(getRequest().getParameter("skuId"));
        WmsWareSku wareSku = new WmsWareSku();
        if (null != id) {
            wareSku = wmsWareSkuService.findById(id);
        } else {
            wareSku.setSkuId(skuId);
            PmsSkuInfo skuInfo = pmsSkuInfoService.findById(skuId);
            if (null == skuInfo) {
                return error(I18nUtil.message(ExceptionConstants.NOT_EXIST, "商品[" + skuId + "]"));
            }
            wareSku.setSkuName(skuInfo.getSkuName());
        }

        return success(wareSku);
    }

    @SubmitPermission
    @PostMapping("/sku/save")
    @AclPermission(code = "mall:ware:edit")
    @ApiOperation(value = "保存商品库存表单数据")
    @ApiImplicitParam(value = "商品库存数据", dataTypeClass = WmsWareSku.class, type = "body")
    public AjaxResponse saveSku() {
        WmsWareSku wmsWareSku;
        try {
            Long id = Convert.toLong(getRequest().getParameter("id"));
            if (null != id) {
                wmsWareSku = wmsWareSkuService.findById(id);
            } else {
                wmsWareSku = new WmsWareSku();
            }
            initDB(wmsWareSku);
            wmsWareSkuService.insertOrUpdate(wmsWareSku);
            return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS), null);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SAVE_FAILURE), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/sku/delete/{id}")
    @AclPermission(code = "mall:ware:delete")
    @ApiOperation(value = "删除仓库数据")
    @ApiImplicitParam(name = "id", value = "主键", type = "path")
    public AjaxResponse deleteSku(@PathVariable String id) throws Exception {
        wmsWareSkuService.delete(id);
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }
}
